using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class shopTank : MonoBehaviour
{
    public Material material_origin;
    private GameObject intergralManageObj;
    private Text shopMany;
    private GameObject tank;
    public GameObject cell_old = null;
    void Start()
    {
        material_origin = new Material(gameObject.transform.GetComponent<Renderer>().material);
        intergralManageObj = GameObject.Find("intergral");
    }

    // Update is called once per frame
    void Update()
    {
        if (intergralManageObj != null)
        {
            shopMany = intergralManageObj.transform.Find("number").GetComponent<Text>();
            //获取interGral值
            Debug.Log("商店可用积分:" + shopMany.text);
        }
    }
    //鼠标点击事件
    private void OnMouseDown()
    {
        int currMoney = int.Parse(shopMany.text);
        if (currMoney>3000)
        {

            currMoney-=3000;
            shopMany.text = currMoney.ToString();
            tank = Instantiate((GameObject)Resources.Load("tank"));
            //实例化tank预制体  

            //遍历所有标签为cell的GameObject 
            foreach (GameObject element in GameObject.FindGameObjectsWithTag("cell"))
            {
                if (element.GetComponent<cell>().selected == true && element.GetComponent<cell>().is_useful == true)
                {
                    //获取找到cell的坐标
                    float base_x = element.transform.position.x;
                    float base_z = element.transform.position.z;
                    float tank_y = element.transform.position.y;
                    //将实例化的tank移动到该cell的位置
                    tank.transform.position = new Vector3(base_x, tank_y, base_z);
                    //将tank的selected设置为false,颜色设置为默认颜色
                    tank.GetComponent<tank>().selected = false;
                    tank.transform.GetComponent<Renderer>().material.color = material_origin.color;
                    tank.transform.Find("Gun").GetComponent<Renderer>().material.color = material_origin.color;
                    //将cell的selected设置为false,is_useful设置为false
                    element.GetComponent<cell>().selected = false;
                    element.GetComponent<cell>().is_useful = false;
                    //如果之前选中的cell不为空,将其is_useful设置为true
                    if (cell_old != null)
                    {
                        cell_old.GetComponent<cell>().is_useful = true;
                    }
                    //将当前选中的cell赋值给之前选中的cell
                    cell_old = element;
                }
            }
        }

    }
    private void OnMouseOver()
    //鼠标悬停时调用
    {
        gameObject.transform.GetComponent<Renderer>().material.color = Color.green;
        gameObject.transform.Find("gun").GetComponent<Renderer>().material.color = Color.green;
        //将Render的材质颜色设置为红色     
    }
    private void OnMouseExit()
    //鼠标退出时调用
    {
        gameObject.transform.GetComponent<Renderer>().material.color = material_origin.color;
        //将Render的材质颜色设置为material_origin的颜色
        gameObject.transform.Find("gun").GetComponent<Renderer>().material.color = material_origin.color;
    }
}
